﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Chenxin.Lib.House.BLL
{
    public class House_TraceInfo
    {
        #region  成员方法

        //*******************************************
        //功能概要       ：写入房源跟单
        //参数説明       ：
        //返回值説明     ：
        //功能詳細       ：写入房源跟单
        //作成者         ：姚志强
        //作成日         ：2010-03-01
        //修改者         ：
        //修改日         ：
        //*******************************************
        public static void AddHouseTraceInfo(Chenxin.Lib.House.Entity.House_TraceInfo traceInfo)
        {
            try
            {
                //添加日志
                Chenxin.Lib.House.DAL.House_TraceInfo.Add(traceInfo);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //*******************************************
        //功能概要       ：根据当前节点类型获取跟单
        //参数説明       ：dicParams-参数列表
        //返回值説明     ：客需列表
        //功能詳細       ：根据当前节点类型获取跟单
        //作成者         ：何基锋
        //作成日         ：2010-08-12
        //*******************************************
        public static DataSet GetTraceListByParams(Dictionary<string, string> dicParams)
        {
            DataSet ds = null;

            int PageIndex = 1;
            int PageSize = 10;

            string OrderField = "House_TraceInfo.TraceTime DESC";
            string TableName = "House_TraceInfo";
            StringBuilder strWhere = new StringBuilder();

            string FieldList = "HouseAddress,TraceTime,Tractor,Depart,TraceContext";
            string TableFieldList = "HouseAddress,TraceTime,Tractor,Depart,TraceContext";

            if (dicParams.ContainsKey("sort"))
            {
                OrderField = dicParams["sort"];
            }

            strWhere.Append(" 1=1 ");

            if (dicParams.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(dicParams["PageIndex"]);
            }
            if (dicParams.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(dicParams["PageSize"]);
            }

            if (dicParams.ContainsKey("StartDate"))
            {
                strWhere.AppendFormat(" AND TraceTime >= '{0}'", dicParams["StartDate"]);
            }

            if (dicParams.ContainsKey("EndDate"))
            {
                strWhere.AppendFormat(" AND TraceTime <= '{0}'", dicParams["EndDate"]);
            }
            

            if (dicParams.ContainsKey("dtype"))
            {
                string DemandType = dicParams["dtype"];
                switch (DemandType)
                {
                    case "housetrace_sale":
                        strWhere.AppendFormat(" AND [Status] = {0} ", 0);
                        strWhere.Append(" AND EXISTS(");
                        strWhere.Append("       SELECT 1");
                        strWhere.Append("       FROM House_BaseInfo H");
                        strWhere.Append("       WHERE House_TraceInfo.HouseId = H.HouseId");
                        strWhere.AppendFormat(" AND (H.BusinessKind={0} OR H.BusinessKind={1})", 10, 30);
                        strWhere.Append("   )");
                        break;
                    case "housetrace_rent":
                        strWhere.AppendFormat(" AND [Status] = {0} ", 0);
                        strWhere.Append(" AND EXISTS(");
                        strWhere.Append("       SELECT 1");
                        strWhere.Append("       FROM House_BaseInfo H");
                        strWhere.Append("       WHERE House_TraceInfo.HouseId = H.HouseId");
                        strWhere.AppendFormat(" AND H.BusinessKind={0}", 20);
                        strWhere.Append("   )");
                        break;
                    case "housetrace_count":
                        strWhere.AppendFormat(" AND [Status] = {0} ", 0);
                        break;
                    case "customertrace_sale":
                        strWhere.AppendFormat(" AND [Status] = {0} ", 1);
                        strWhere.Append(" AND EXISTS(");
                        strWhere.Append("       SELECT 1");
                        strWhere.Append("       FROM Customer_DemandInfo D");
                        strWhere.Append("       WHERE House_TraceInfo.HouseId = D.CustomerId");
                        strWhere.AppendFormat(" AND D.DemandKind={0}", 10);
                        strWhere.Append("   )");
                        break;
                    case "customertrace_rent":
                        strWhere.AppendFormat(" AND [Status] = {0} ", 1);
                        strWhere.Append(" AND EXISTS(");
                        strWhere.Append("       SELECT 1");
                        strWhere.Append("       FROM Customer_DemandInfo D");
                        strWhere.Append("       WHERE House_TraceInfo.HouseId = D.CustomerId");
                        strWhere.AppendFormat(" AND D.DemandKind={0}", 20);
                        strWhere.Append("   )");
                        break;
                    case "customertrace_count":
                        strWhere.AppendFormat(" AND [Status] = {0} ", 1);
                        strWhere.Append(" AND EXISTS( ");
                        strWhere.Append(" SELECT 1 ");
                        strWhere.Append(" FROM Customer_DemandInfo ");
                        strWhere.Append(" WHERE House_TraceInfo.HouseId = Customer_DemandInfo.CustomerId");
                        strWhere.Append(" ) ");
                        break;
                }
            }

            if (dicParams.ContainsKey("ParentType"))
            {
                string ParentType = dicParams["ParentType"];
                switch (ParentType)
                {
                    case "0":
                        strWhere.AppendFormat("AND CityId='{0}'", dicParams["ParentId"]);
                        break;
                    case "10":
                        strWhere.AppendFormat("AND CompanyId='{0}'", dicParams["ParentId"]);
                        break;
                    case "20":
                        strWhere.Append("AND EXISTS(");
                        strWhere.Append("       SELECT 1");
                        strWhere.Append("       FROM Chenxin_Main.dbo.Hr_Struct_DepartTreeRelation R");
                        strWhere.Append("       WHERE House_TraceInfo.DepartId = R.ChildId");
                        strWhere.AppendFormat(" AND ParentId='{0}'", dicParams["ParentId"]);
                        strWhere.Append("   )");
                        break;
                    case "25":
                    case "30":
                        strWhere.AppendFormat("AND DepartId='{0}'", dicParams["ParentId"]);
                        break;
                    case "40":
                        strWhere.AppendFormat("AND TractorId='{0}' and DepartId='{1}'", dicParams["ParentId"], dicParams["FatherId"]);
                        break;
                    default:
                        break;
                }
            }

            ds = Chenxin.Lib.House.DAL.House_TraceInfo.GetList(PageIndex, PageSize, OrderField, TableName, strWhere.ToString(), FieldList, TableFieldList, true);
            return ds;
        }
        #endregion 成员方法
    }
}
